﻿using AdinZ.DB;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;


/**
* @Author: AdinZ
* @Q  Q : 1454269
* @Email:adinz@adinz.com
* @Created: 2014/5/26 22:41:31
*
* */
namespace Novel.Sys.Dal
{
    public class novel_location
    {
        public int Add(Model.novel_location model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("INSERT INTO [novel_location] ([UID], [IMEI], [Latitude], [Longitude], [update],[address],[province],[city],[district]) VALUES (");
            strSql.Append("@UID,@IMEI,@Latitude,@Longitude,@update,@address,@province,@city,@district");
            strSql.Append(");select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@UID", SqlDbType.Int),
                    new SqlParameter("@IMEI", SqlDbType.NVarChar),
                    new SqlParameter("@Latitude", SqlDbType.Float),
                    new SqlParameter("@Longitude", SqlDbType.Float),
                    new SqlParameter("@update", SqlDbType.DateTime),
                    new SqlParameter("@address", SqlDbType.NVarChar),
                    new SqlParameter("@province", SqlDbType.NVarChar),
                    new SqlParameter("@city", SqlDbType.NVarChar),                    
                    new SqlParameter("@district", SqlDbType.NVarChar)                  };
            parameters[0].Value = model.UID;
            parameters[1].Value = model.IMEI;
            parameters[2].Value = model.Latitude;
            parameters[3].Value = model.Longitude;
            parameters[4].Value = model.Update;
            parameters[5].Value = model.Address;
            parameters[6].Value = model.Province;
            parameters[7].Value = model.City;
            parameters[8].Value = model.District;
            object obj = DbHelpers.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        public int Update(Model.novel_location model)
        {
            string sql = "UPDATE [novel_location] SET [UID]='" + model.UID + "', [IMEI]='" + model.IMEI + "', [Latitude]='" + model.Latitude + "', [Longitude]='" + model.Longitude + "', [update]='" + model.Update + "' WHERE ([ID]='" + model.ID + "')";
            return DbHelpers.NonQuery(sql);
        }

        public bool Exists(int UID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [novel_location]");
            strSql.Append(" where UID=@UID");
            SqlParameter[] parameters = {
                    new SqlParameter("@UID", SqlDbType.Int)};
            parameters[0].Value = UID;
            return DbHelpers.Exists(strSql.ToString(), parameters);
        }
        public bool Exists(string IMEI)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [novel_location]");
            strSql.Append(" where IMEI=@IMEI");
            SqlParameter[] parameters = {
                    new SqlParameter("@IMEI", SqlDbType.NVarChar)};
            parameters[0].Value = IMEI;
            return DbHelpers.Exists(strSql.ToString(), parameters);
        }
        public Model.novel_location GetModel(string IMEI)
        {
            Model.novel_location model = new Model.novel_location();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from [novel_location] where IMEI=@IMEI");
            SqlParameter[] parameters = {
                    new SqlParameter("@IMEI", SqlDbType.NVarChar)};
            parameters[0].Value = IMEI;
            DataSet ds = DbHelpers.GetDataSet(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UID"] != null && ds.Tables[0].Rows[0]["UID"].ToString() != "")
                {
                    model.UID = int.Parse(ds.Tables[0].Rows[0]["UID"].ToString());
                }
                else model.UID = 0;
                if (ds.Tables[0].Rows[0]["IMEI"] != null && ds.Tables[0].Rows[0]["IMEI"].ToString() != "")
                {
                    model.IMEI = ds.Tables[0].Rows[0]["IMEI"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Latitude"] != null && ds.Tables[0].Rows[0]["Latitude"].ToString() != "")
                {
                    model.Latitude = double.Parse(ds.Tables[0].Rows[0]["Latitude"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Longitude"] != null && ds.Tables[0].Rows[0]["Longitude"].ToString() != "")
                {
                    model.Longitude = double.Parse(ds.Tables[0].Rows[0]["Longitude"].ToString());
                }
                if (ds.Tables[0].Rows[0]["update"] != null && ds.Tables[0].Rows[0]["update"].ToString() != "")
                {
                    model.Update = DateTime.Parse(ds.Tables[0].Rows[0]["update"].ToString());
                }
 
                return model;
            }
            else
                return null;
        }
        public Model.novel_location GetModel(int UID)
        {
            Model.novel_location model = new Model.novel_location();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from [novel_location] where UID=@UID");
            SqlParameter[] parameters = {
                    new SqlParameter("@UID", SqlDbType.Int)};
            parameters[0].Value = UID;
            DataSet ds = DbHelpers.GetDataSet(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UID"] != null && ds.Tables[0].Rows[0]["UID"].ToString() != "")
                {
                    model.UID = int.Parse(ds.Tables[0].Rows[0]["UID"].ToString());
                }
                else model.UID = 0;
                if (ds.Tables[0].Rows[0]["IMEI"] != null && ds.Tables[0].Rows[0]["IMEI"].ToString() != "")
                {
                    model.IMEI = ds.Tables[0].Rows[0]["IMEI"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Latitude"] != null && ds.Tables[0].Rows[0]["Latitude"].ToString() != "")
                {
                    model.Latitude = double.Parse(ds.Tables[0].Rows[0]["Latitude"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Longitude"] != null && ds.Tables[0].Rows[0]["Longitude"].ToString() != "")
                {
                    model.Longitude = double.Parse(ds.Tables[0].Rows[0]["Longitude"].ToString());
                }
                if (ds.Tables[0].Rows[0]["update"] != null && ds.Tables[0].Rows[0]["update"].ToString() != "")
                {
                    model.Update = DateTime.Parse(ds.Tables[0].Rows[0]["update"].ToString());
                }

                return model;
            }
            else
                return null;
        }

    
    }
}
